package com.demo.springcloud.controller;


import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/hytrix")
public class HystrixController {

//    @HystrixCommand(defaultFallback = "fallback",commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
//    } )

    /**
     * 当在15s之内，有4次连续的failed，但是
     * @param command
     * @return
     * @throws InterruptedException
     */
    @HystrixCommand(defaultFallback = "fallback",commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "15000"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "40"),
    } )
    @RequestMapping("/command")
    @ResponseBody
    public String hello(int command) throws InterruptedException {
        if(command == 0){
//            Thread.currentThread().sleep(2000);
            return "hello";
        }else {
            throw new RuntimeException("some exception");
        }

    }

    public String fallback(){
        return "failed";
    }
}
